package com.kai;

import com.kai.dao.EmpMapper;
import com.kai.pojo.Emp;
import com.kai.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * Created By ShaoXikai on 2022/5/28
 */
public class ContextTest {

    //     练习1: 查询emp表中的所有员工信息
    @Test
    public void testFindAll() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.findAll();
        emps.forEach(System.out::println);
    }

    //     练习2: 新增员工信息: 赵云 保安 6000
    @Test
    public void testInsertZhaoYun() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        int i = mapper.insertZhaoYun();
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    // 练习3：修改员工信息：赵云 保镖 20000
    @Test
    public void testUpdateZhaoYun() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        int i = mapper.updateZhaoYun();
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    //     练习4: 删除name为'赵云'的记录
    @Test
    public void testDeleteZhaoYun() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        int i = mapper.deleteZhaoYun();
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    //     练习5: 查询emp表中指定id的员工信息
    @Test
    public void testSelectById() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        System.out.println(mapper.selectUserById(6L));
        session.close();
    }

    //     练习6: 新增员工信息: 张飞 Java开发工程师 15000
    @Test
    public void testInsertZhangfei() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setName("张飞");
        emp.setJob("架构师");
        int i = mapper.insertUser(emp);
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    //     练习7: 修改员工信息: 张飞 架构师 25000
    @Test
    public void updateZhangFei() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setId(14L);
        emp.setName("张飞");
        emp.setJob("架构师");
        int i = mapper.updateUser(emp);
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    //     练习8：删除emp表中指定id的员工信息
    @Test
    public void selectById() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        int i = mapper.deleteUser("张飞");
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();
    }

    //     练习9: 动态指定要显示的列（name,salary）
    @Test
    public void selectSomeColumn() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.selectSomeCol("name,salary");
        emps.forEach(System.out::println);
        session.close();
    }

    //     练习10: 根据name模糊查询emp表(查询所有姓刘的员工)
    @Test
    public void selectByFirstName() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.selectByFirstName("刘");
        emps.forEach(System.out::println);
        session.close();
    }

    //     练习11: 根据name模糊查询emp表(查询名字中包含涛字的员工)
    @Test
    public void selectByFullName() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.selectByFullName("涛");
        emps.forEach(System.out::println);
        session.close();
    }

    //     练习12: 根据薪资查询员工信息(查询员工薪资大于3000小于4000的员工信息)
    @Test
    public void selectBySalary() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.selectBySalary(3000, 4000);
        emps.forEach(System.out::println);
        session.close();
    }

    //     练习13: 根据员工的id批量删除员工信息（删除id为2,4,6,8的员工信息）
    @Test
    public void deleteByIds() {
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Long[] ids = {2L, 4L, 6L, 8L};
        int i = mapper.deleteByIds(ids);
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        session.commit();
        session.close();

    }

    //     练习14：根据员工的id批量查询指定id的员工信息(查询id为1,3,5的员工信息)
    @Test
    public void selectByIds(){
        SqlSession session = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Long[] ids = {1L, 3L, 5L};
        List<Emp> emps = mapper.selectByIds(ids);
        emps.forEach(System.out::println);
        session.close();
    }

}
